package com.web.kdl.service.createTable;

import com.web.kdl.util.TableNameUtils;
import org.springframework.stereotype.Component;

@Component
public class ControlTable{
    public String[]  getControlTableName(String SiteName,String MatrixName) {
        String[] createTableSql = new String[3];
        String controlDataTableName = TableNameUtils.getControlDataTableName(SiteName,MatrixName);
        String controlAlertTableName = TableNameUtils.getControlAlertTableName(SiteName,MatrixName);
        String controlStatusTableName = TableNameUtils.getControlStatusTableName(SiteName,MatrixName);
        createTableSql[0] = controlDataTableName;
        createTableSql[1] = controlAlertTableName;
        createTableSql[2] = controlStatusTableName;
        return createTableSql;
    }


    public String getDeleteSql(String SiteTableName) {
        return String.format("DROP TABLE IF EXISTS `%s`;", SiteTableName);
    }
    public String getCreateDataTableSql(String SiteTableName) {
        return String.format("CREATE TABLE IF NOT EXISTS `%s`  (" +
                "`id` int NOT NULL AUTO_INCREMENT," +
                "`sum_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
                "`sub_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
                "`time` datetime NULL DEFAULT NULL," +
                "`month` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '月份 例如:2025-06',"+
                "`data_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
                "`data_value` double(32, 2) NULL DEFAULT NULL," +
                "`unit` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
                "`device_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
                "PRIMARY KEY (`id`) USING BTREE" +
                ") ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;", SiteTableName);
    }
    public String getCreateAlertTableSql(String SiteTableName) {
        return String.format("CREATE TABLE IF NOT EXISTS  `%s` (" +
                "  `id` int NOT NULL AUTO_INCREMENT," +
                "  `sum_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `sub_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `flywheel_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `device_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `alert_level` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `alert_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `alert_value` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `time` datetime DEFAULT NULL," +
                "  PRIMARY KEY (`id`) USING BTREE" +
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;", SiteTableName);
    }

    public String getCreateStatusTableSql(String SiteTableName) {
        return String.format("CREATE TABLE IF NOT EXISTS `%s` (" +
                "  `id` int NOT NULL AUTO_INCREMENT," +
                "  `flywheel_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `sum_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `sub_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `device_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `status_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `status_value` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `status_byte_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL," +
                "  `time` datetime DEFAULT NULL," +
                "  PRIMARY KEY (`id`) USING BTREE" +
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;", SiteTableName);
    }
}
